package com.hot12345.job;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;

/**
 * 任务调度触发器
 */
public class JobTrigger {
	private static Scheduler scheduler;
	private static String DEFAULT_GROUP = Scheduler.DEFAULT_GROUP;
	private static Log log = LogFactory.getLog(JobTrigger.class);
	
	/**
	 * 初始化
	 */
	public static void init(){
		try{
			scheduler = StdSchedulerFactory.getDefaultScheduler();
			scheduler.start();
			if(log.isDebugEnabled()){
				log.debug("调度触发器启动成功。");
			}
		}catch(Exception e){
			if(log.isErrorEnabled())
			log.error("调度触发器启动异常。",e);
		}
	}
	
	/**
	 * 加入一个调度对象(马上执行该任务)
	 * 
	 * @param jobName 调度名称
	 * @param classPath 类路径
	 * @throws Exception 
	 */
	public static boolean appendJobToExcute(String jobName,String classPath) throws Exception{
		boolean flag = false;
		try{
			String group = "SimpleTrigger";
			@SuppressWarnings("rawtypes")
			Class cName = Class.forName(classPath);
			JobDetail jobDetail = new JobDetail(jobName,group,cName);
			jobDetail.getJobDataMap().put("jobName",jobName);
			
	        Trigger trigger = new SimpleTrigger(jobName,group);
			scheduler.scheduleJob(jobDetail,trigger);
			if(log.isDebugEnabled())
			log.warn("加入调度对象成功：jobName="+jobName);
			flag = true;
		}catch (Exception e) {
			String errorMsg="执行调度对象异常：jobName="+jobName;
			if(log.isErrorEnabled())
			log.error(errorMsg,e);
			throw new Exception(errorMsg+e.getMessage());
		}
		return flag;
	}
	
	/**
	 * 加入一个调度对象
	 * @param jobName 调度名称
	 * @param cron 执行时间
	 * @param classPath  执行类路径
	 * @throws Exception 
	 */
	public static boolean appendJobByClassPath(String jobName,String cron,String classPath) throws Exception{
		boolean flag = false;
		try{
			//String packageName = ConstantsUtil.DEFAULT_PACKAGE_PATH;
			@SuppressWarnings("rawtypes")
			Class cName = Class.forName(classPath);
			JobDetail jobDetail = new JobDetail(jobName,DEFAULT_GROUP,cName);
			jobDetail.getJobDataMap().put("jobName",jobName);
			
			CronTrigger trigger = new CronTrigger(jobName,DEFAULT_GROUP,cron);
			scheduler.scheduleJob(jobDetail,trigger);
			if(log.isDebugEnabled()){
				log.debug("加入调度对象成功：jobName="+jobName+",cron="+cron);
			}
			flag = true;
			
		}catch (Exception e) {
			String errorMsg="加入调度对象异常：jobName="+jobName+",cron="+cron;
			if(log.isErrorEnabled()){
				log.error(errorMsg,e);
			}
			throw new Exception(errorMsg+e.getMessage());
		}
		return flag;
	}
	
	
	/**
	 * 删除一个调度对象
	 * @param jobName 调度名称
	 * @throws Exception 
	 */
	public static boolean removeJob(String jobName) throws Exception{
		boolean flag = false;
		try {
			flag = scheduler.deleteJob(jobName,DEFAULT_GROUP);
			if(log.isDebugEnabled())
			log.debug("删除调度对象成功：jobName="+jobName);
		} catch (Exception e) {
			String errorMsg="删除调度对象异常：jobName="+jobName;
			if(log.isErrorEnabled())
			log.error(errorMsg,e);
			throw new Exception(errorMsg+e.getMessage());
		}
		return flag;
	}
	
	
	/**
	 * 是否存在一个调度对象
	 * @param jobName 调度名称
	 * @throws Exception 
	 */
	public static boolean isExsitsJob(String jobName) throws Exception{
		boolean flag = false;
		try {
			JobDetail job = scheduler.getJobDetail(jobName,DEFAULT_GROUP);
			if(job != null){
				flag = true;
			}else{
				flag = false;
			}
		} catch (Exception e) {
			String errorMsg="删除调度对象异常：jobName="+jobName;
			if(log.isErrorEnabled())
			log.error(errorMsg,e);
			throw new Exception(errorMsg+e.getMessage());
		}
		return flag;
	}
	/**
	 * 关闭调度器
	 */
	public static void destroy() {
		try {
			if (scheduler != null){
				scheduler.shutdown(true);
			}
		} catch (Exception e) {
			if(log.isErrorEnabled())
			log.error("销毁调度异常。", e);
		}
	}
}
